package com.ptswitch.material.web.action;

import java.util.List;

import org.springframework.stereotype.Controller;

import com.ptswitch.material.model.Uzer;
import com.ptswitch.material.util.Constant;

@Controller("userAction")
public class UserAction extends ActionSupport{

	private String like;
	private Uzer user;
	
	// display
	public String display() {
		user = userSer.get(id);
		return "display";
	}

	public String all() {
		mustAdmin();
		List users = userSer.getAll(page.getCurrentRow(), page.getPageSize());
		page.setRowCount(userSer.countAll());
		display.put("users", users);
		display.put("emails", eMailAddressSer.getAll());
		return "list";
	}

	// to edit
	public String toAdd() {
		mustAdmin();
		user = null;
		return "add";
	}

	public String toUpdate() {
		mustAdmin();
		user = userSer.get(id);
		return "update";
	}

	// save
	public String add() {
		mustAdmin();
		user.md5password();
		userSer.save(user);
		putMsg("保存用户成功");
		hlogSer.log(visitor(), Constant.addUser,user);
		user=null;
		return "rList";
	}

	public String update() {
		mustAdmin();
		userSer.save(user);
		putMsg("保存用户成功");
		hlogSer.log(visitor(), Constant.updateUser,user);
		user=null;
		return "rList";
	}
	
	public String changePassword() {
		mustAdmin();
		user.md5password();
		userSer.save(user);
		hlogSer.log(visitor(), Constant.updateUserPassword,user);
		putMsg("修改密码成功");
		return "rList";
	}

	// delete
	public String delete() {
		mustAdmin();
		userSer.delete(id);
		putMsg("删除用户成功");
		hlogSer.log(visitor(), Constant.deleteUser,user);
		return "rList";
	}
	
	public String deleteAll(){
		mustAdmin();
		userSer.deleteAll();
		hlogSer.log(visitor(), Constant.deleteAllUser);
		return "rList";
	}
	
	// get and set
	public String getLike() {
		return like;
	}

	public void setLike(String like) {
		this.like = like;
	}

	public Uzer getUser() {
		if(user==null){
			user=new Uzer();
		}
		return user;
	}

}
